package com.guoqing.example.exception;

import com.guoqing.example.common.MyEnum;
import com.guoqing.example.common.Result;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.Serializable;

/**
 * @ClassName MyExceptionHandler
 * @Description TODO
 * @Author ZhangGuoQing
 * @Date 2020/8/19/0019 21:45
 */
@RestControllerAdvice
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyExceptionHandler extends RuntimeException implements Serializable {

    private String code;
    private String message;

    public MyExceptionHandler(MyEnum myEnum) {
        this.code = myEnum.getCode();
        this.message = myEnum.getMessage();
    }

    @ExceptionHandler(AuthorizationException.class)
    public Result exceptionHandler(AuthorizationException e) {
        String a = "AuthorizationException 没有通过权限验证！     ";
        return Result.buildFailed(MyEnum.FAILED.getCode(), a + e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result exceptionHandler(RuntimeException e) {
        log.info("RuntimeException！", e);
        return Result.buildFailed(MyEnum.FAILED.getCode(), "RuntimeException   " + e.getMessage());
    }

    @ExceptionHandler(MyExceptionHandler.class)
    public Result exceptionHandler(MyExceptionHandler e) {
        log.info("RuntimeException！", e);
        return Result.buildFailed(MyEnum.FAILED.getCode(), "MyExceptionHandler   " + e.getMessage());
    }

}
